<?php
/**
 * User: aka
 * Time: 19:54
 */

namespace app\write\controller;

use app\validate\users\SendComment;
use app\validate\users\SignUp;
use think\facade\Session;
use think\Request;

class Users
{
    private $gameState = 1;
    private $userId;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
    }

    public function __destruct()
    {
        unset(
            $this->gameState,
            $this->userId
        );
    }

    public function signUp(Request $request)
    {
        $validate = new SignUp();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $emailBool = check_same_email($request->param('email'));
            if ($emailBool == true)
            {
                return json(['state' => 601]);
            }
            $gameIdBool = check_same_game_id($request->param('game_id'));
            if ($gameIdBool == true)
            {
                return json(['state' => 602]);
            }
            \app\tenno\model\Users::create([
               'email' => $request->param('email'),
               'game_id' => $request->param('game_id'),
               'password' => get_encode_password($request->param('password')),
               'game_state' => $this->gameState
            ]);
            return json(['state' => 200]);
        }
    }

    public function sendComment(Request $request)
    {
        $validate = new SendComment();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $commentUserId = base64_decode($request->param('comment_user_id'));
            \app\tenno\model\UsersComment::create([
                'uid' => $commentUserId,
                'comment_content' => $request->param('comment_content'),
                'comment_uid' => $this->userId
            ]);
            $commentUserInfo = \app\tenno\model\Users::userInfo($this->userId)->find();
            return json([
                'state' => 200,
                'comment_game_id' => $commentUserInfo['game_id'],
                'now_time' => get_time()
            ]);
        }
    }
}